import BaseModel from './BaseModel'
import { get } from '../../api/ajax'
import API from '../../api/apis'
import WorkModel from './WorkModel'
import Filters from '../../utils/filters'

/**
 * 我的模型
 */
class Me extends BaseModel {

  constructor() {
    super()
    /**
     * 主页头像
     * @type {string}
     */
    this.home_icon = ''
    /**
     * 主页标题
     * @type {string}
     */
    this.home_title = ''
    /**
     * 简历链接
     * @type {string}
     */
    this.home_jianli = ''
    /**
     * 关于我
     * @type {string}
     */
    this.abouts = ''
    /**
     * 关于我切割换行后的数组字符串
     * @type {Array}
     */
    this.aboutArr = []
    /**
     * 专业技能
     * @type {string}
     */
    this.expertise = ''
    this.expertiseArr = []
    /**
     * ios
     * @type {number}
     */
    this.iosNum = 0
    /**
     * node
     * @type {number}
     */
    this.nodeNum = 0
    /**
     * 前端
     * @type {number}
     */
    this.webNum = 0
    /**
     * 小程序
     * @type {number}
     */
    this.xcxNum = 0
    /**
     * Java
     * @type {number}
     */
    this.javaNum = 0
    /**
     * iOS技能说明
     * @type {string}
     */
    this.iosShow = ''
    /**
     * 前端技能说明
     * @type {string}
     */
    this.webShow = ''
    /**
     * Node技能说明
     * @type {string}
     */
    this.nodeShow = ''
    /**
     * 小程序技能说明
     * @type {string}
     */
    this.xcxShow = ''
    /**
     * java技能说明
     * @type {string}
     */
    this.javaShow = ''
    /**
     * 工作经验说明
     * @type {string}
     */
    this.experience_show = ''
    this.experience_showArr = []
    /**
     * works
     * @type {Array}
     */
    this.works = []
    /**
     * 工作经历说明
     * @type {string}
     */
    this.works_show = ''
    this.works_showArr = []

    /**
     * ====================== URL 相关 ====================================
     */
    /**
     * 更新数据的URL
     * @type {String}
     */
    this.updateUrl = API.me.update
    /**
     * 添加修改数据的KEY数组
     * @type {Array}
     */
    this.addOrUpdateKeyArr = ['home_icon', 'home_title', 'home_jianli',
      'abouts', 'expertise', 'iosNum', 'nodeNum', 'webNum',
      'xcxNum', 'javaNum', 'iosShow', 'webShow', 'nodeShow', 'xcxShow', 'javaShow',
      'experience_show', 'works_show']
  }

  /* ----- 获取指定图片大小 -------*/
  /**
   * 手机端首页图片
   * @returns {*|string}
   */
  get mMeHomeIconUrl() {
    return Filters.imageFill(this.home_icon, 3, 3, 'rem')
  }

  /**
   * PC端首页图片
   * @returns {*|string}
   */
  get pcMeHomeIconUrl() {
    return Filters.imageFill(this.home_icon, 150, 150)
  }

  /**
   * 加载个人数据
   * @returns {Promise<any>}
   */
  getModel(cache) {
    return new Promise((resolve, reject) => {
      get(API.me.get, null, cache).then((data) => {
        this.setDataWithJson(data)
        this.aboutArr = data.abouts.split('\n')
        this.expertiseArr = data.expertise.split('\n')
        this.works_showArr = data.works_show.split('\n')
        this.experience_showArr = data.experience_show.split('\n')
        this.works = WorkModel.getModelsWithArr(data.works)
        resolve(this)
      }).catch((err) => {
        reject(err)
      })
    })
  }
}

export default Me
